% Test the function makeppft filter.
%
% Yoel Shkolnisky, December 2010.
function testMakeHFilter3D(figure_flag)

if nargin < 1
  figure_flag = 0;
end

precision='double';
size_list=2*unique(round(logspace(0,6.5*log(2)/log(10))));
size_list=size_list(size_list>50);
error_list=zeros([2,size(size_list,2)]);
speedup_list=zeros([2,size(size_list,2)]);

speedup_fn_no_precond='3D_speedup_without_precond';
error_fn_no_precond='3D_error_without_precond';
speedup_fn_with_precond='3D_speedup_with_precond';
error_fn_with_precond='3D_error_with_precond';

for precond=0:1    
    idx = 0;
    if (precond)
        speedup_fn=speedup_fn_with_precond;
        error_fn=error_fn_with_precond;
    else
        speedup_fn=speedup_fn_no_precond;
        error_fn=error_fn_no_precond;
    end
    for n=size_list
        idx = idx +1;
        A=rand(n,n,n,precision);
        if precond
            tic;B=preconditionedAdjointPPFT3D(optimizedPPFT3D(A,precision),precision);t1=toc;
        else
            tic;B=optimizedAdjointPPFT3D(optimizedPPFT3D(A,precision),precision);t1=toc;
        end
        
        L=3*n-2; %n+2n-1-1
        filename=sprintf('filters%sH3Dfilter%dprecond%dprecision%s.dat',filesep,n,precond,precision);
        if exist(filename,'file')
            H=load3DHFilter(filename,precision);
        else
            H=save3DHFilter(filename,n,precond,precision);
        end

        Hhat=fftn(H.filter,[L L L]);

        tic;
        Ahat=fftn(A,[L L L]);
        Cc=ifftn(Ahat.*Hhat,[L L L]);
        Cc=Cc(n:2*n-1,n:2*n-1,n:2*n-1);
        t2=toc;
        error_list(precond+1,idx)=(norm(B(:)-Cc(:))./norm(B(:)));
        speedup_list(precond+1,idx)=t1/t2;
        reportTestResult(n,error_list(precond+1,idx),speedup_list(precond+1,idx),precision);   
    end
    if (figure_flag)
        figure('name',speedup_fn);
        plot(size_list,speedup_list(precond+1,:));
        save_pic(speedup_fn)
        figure('name',error_fn);
        plot(size_list,error_list(precond+1,:));
        save_pic(error_fn)
    end
end
end
